package com.hnyfkj.jyindustry.common.config;

import com.hnyfkj.jyindustry.common.constant.Constant;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.ParameterBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.schema.ModelRef;
import springfox.documentation.service.*;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spi.service.contexts.SecurityContext;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

import java.util.ArrayList;
import java.util.List;

/**
 * 
 * @author admin
 *
 */

@Configuration
@EnableSwagger2
public class IndustrySwaggerConfig {

	@Bean
	public Docket totalApi() {
		final List<SecurityScheme> list = new ArrayList<>();
		list.add(new ApiKey(Constant.HEADER_TOKEN, Constant.HEADER_TOKEN, "header"));
		return new Docket(DocumentationType.SWAGGER_2).groupName("API").apiInfo(apiInfo()).select()
				.apis(RequestHandlerSelectors.basePackage("com.hnyfkj.jyindustry.biz")).paths(PathSelectors.any())
				.build().securitySchemes(list).securityContexts(securityContexts()).apiInfo(apiInfo());
	}

//	@Bean
//	public Docket agriculturalrecordlistApi() {
//		final List<SecurityScheme> list = new ArrayList<>();
//		list.add(new ApiKey("hnyfkjssosessionid", "hnyfkjssosessionid", "header"));
//		return new Docket(DocumentationType.SWAGGER_2).groupName("农事记录API").apiInfo(apiInfo()).select()
//				.apis(RequestHandlerSelectors.basePackage("com.hnyfkj.agriculturalpark.biz.agriculturalrecordlist.controller")).paths(PathSelectors.any())
//				.build().securitySchemes(list).securityContexts(securityContexts()).apiInfo(apiInfo());
//	}

	private List<Parameter> setHeaderToken() {
		ParameterBuilder tokenPar = new ParameterBuilder();
		List<Parameter> pars = new ArrayList<>();
		tokenPar.name(Constant.HEADER_TOKEN).description(Constant.HEADER_TOKEN).modelRef(new ModelRef("string")).parameterType("header").required(false).build();
		pars.add(tokenPar.build());
		return pars;
	}


	private List<SecurityContext> securityContexts() {
		List<SecurityContext> securityContexts=new ArrayList<>();
		securityContexts.add(
				SecurityContext.builder()
						.securityReferences(defaultAuth())
						.forPaths(PathSelectors.regex("^(?!auth).*$"))
						.build());
		return securityContexts;
	}

	private List<SecurityReference> defaultAuth() {
		AuthorizationScope authorizationScope = new AuthorizationScope("global", "accessEverything");
		AuthorizationScope[] authorizationScopes = new AuthorizationScope[1];
		authorizationScopes[0] = authorizationScope;
		List<SecurityReference> securityReferences = new ArrayList<>();
		securityReferences.add(new SecurityReference(Constant.HEADER_TOKEN, authorizationScopes));
		return securityReferences;
	}



	private ApiInfo apiInfo() {
		return new ApiInfoBuilder().title("济源工业云管理平台API").description("Swagger2").version("1.0")
				.build();
	}

}
